package cn.dakaqi.web.api.v1;

import cn.dakaqi.utils.JsonResult;
import cn.dakaqi.utils.rongYun.ApiHttpClient;
import cn.dakaqi.utils.rongYun.PushActivityMesssage;
import cn.dakaqi.utils.rongYun.PushMessageUtil;
import cn.dakaqi.utils.rongYun.models.*;
import com.wordnik.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springside.modules.web.MediaTypes;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/4/7.
 * 用户
 */
@RestController
@RequestMapping(value = "/api/v1/rongYun")
@Slf4j
@Api(value = "融云相关API")
public class RongYunImController
{
    public static String KEY = "bmdehs6pdo82s";//
    public static String SECRET = "odhxkmxFIauAv";//
    @RequestMapping(value = "/guangbo", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResponseEntity<JsonResult> guangbo()
    {
        JsonResult jsonResult = new JsonResult();
        try
        {
            SdkHttpResult result = ApiHttpClient.broadcastMessage(KEY, SECRET, "10",
                    new TxtMessage("guangbo"), "pushContent",
                    "pushData", FormatType.json);
            jsonResult.setCode(0);
            jsonResult.setMessage("OK");
            jsonResult.setData(result);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new ResponseEntity<JsonResult>(jsonResult, HttpStatus.OK);
    }

    @RequestMapping(value = "/tuisong", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResponseEntity<JsonResult> tuisong()
    {
        JsonResult jsonResult = new JsonResult();
        PushMessage message = new PushMessage();
        try
        {
            message = createPushMessage();//融云消息
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        try
        {
            SdkHttpResult result = ApiHttpClient.push(KEY, SECRET, message, FormatType.json);
            jsonResult.setCode(0);
            jsonResult.setMessage("OK");
            jsonResult.setData(result);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new ResponseEntity<JsonResult>(jsonResult, HttpStatus.OK);
    }

    @RequestMapping(value = "/liaotian", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResponseEntity<JsonResult> liaotian()
    {
        JsonResult jsonResult = new JsonResult();
        List<String> toIds = new ArrayList<String>();
        toIds.add("218659");
        toIds.add("218686");
        toIds.add("218676");


        try
        {
            SdkHttpResult result = ApiHttpClient.publishMessage(KEY, SECRET, "10", toIds,
                    new TxtMessage("liaotian"), "pushContent",
                    "pushData", FormatType.json);
            jsonResult.setCode(0);
            jsonResult.setMessage("OK");
            jsonResult.setData(result);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new ResponseEntity<JsonResult>(jsonResult, HttpStatus.OK);
    }

    /**
     * 创建发送落地消息的内容
     */
    private static PushMessage createPushMessage()
            throws UnsupportedEncodingException
    {
        List<String> osList = new ArrayList<String>();
        osList.add("ios");
        osList.add("android");

        TagObj tag = new TagObj();
        tag.setIs_to_all(false);//给全量用户发 设置为true则下面标签和userids将无效

        //给打标签的发则设置标签
        List<String> tagas = new ArrayList<String>();
        tagas.add("a");
        tagas.add("b");
        tagas.add("3");
        tag.setTag(tagas);

        //给特定用户ID发,优先级高于上面的标签
        List<String> tagus = new ArrayList<String>();
        tagus.add("218659");
        tagus.add("218686");
        tagus.add("218676");
        tag.setUserid(tagus);

        PushMessage pmsg = new PushMessage();
        pmsg.setPlatform(osList);
        PushNotification noti = new PushNotification();
        noti.setAlert("tuisong");
        noti.setAndroid(createPush());
        noti.setIos(createPush());
        pmsg.setNotification(noti);

        //下面两个参数的有无 控制发送的落地消息还是不落地的push
        //PushMessage实体中的 fromuserid 和 message为null即为不落地的push
        pmsg.setFromuserid("10");
        MsgObj msg = new MsgObj();
        TxtMessage message = new TxtMessage("hello", "one tuisong");
        msg.setContent(message.toString());
        msg.setObjectName(message.getType());
        pmsg.setMessage(msg);
        //上面两个参数的有无 控制发送的落地消息还是不落地的push

        pmsg.setAudience(tag);
        System.out.println(pmsg.toString());
        return pmsg;
    }

    private static PlatformNotification createPush()
    {
        PlatformNotification data = new PlatformNotification();
        data.setAlert("override alert");
        Map<String, String> map = new HashMap<String, String>();
        map.put("id", "1");
        map.put("name", "2");
        data.setExtras(map);
        return data;
    }

    @RequestMapping(value = "/systemMessage", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResponseEntity<JsonResult> system()
    {
        JsonResult jsonResult = new JsonResult();
        Map<String, String> map = new HashMap<String, String>();
        map.put("activityCode", "A310201606089156");
        map.put("type", PushActivityMesssage.TYPE_ACTIVITY_SIGN_PAGE);
        List<String> toUsers = new ArrayList<String>();
        toUsers.add("402882f654e5b9e70154e5b9e71c0000");
        try
        {
            SdkHttpResult result = PushMessageUtil.publishSystemMessage("内容", map, "40288289540eed3501540eed35940000", toUsers);
            jsonResult.setCode(0);
            jsonResult.setMessage("OK");
            jsonResult.setData(result);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new ResponseEntity<JsonResult>(jsonResult, HttpStatus.OK);
    }

    @RequestMapping(value = "/pushMessage", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResponseEntity<JsonResult> pushMessage()
    {
        JsonResult jsonResult = new JsonResult();
        Map<String, String> map = new HashMap<String, String>();
        map.put("id", "1");
        map.put("name", "2");
        List<String> toUsers = new ArrayList<String>();
        toUsers.add("402882f654e5b9e70154e5b9e71c0000");
        try
        {
            SdkHttpResult result = PushMessageUtil.pushMessage("小助手推送的内容", map, "40288289540eed3501540eed35940000", toUsers);
            jsonResult.setCode(0);
            jsonResult.setMessage("OK");
            jsonResult.setData(result);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new ResponseEntity<JsonResult>(jsonResult, HttpStatus.OK);
    }
}
